//
//  SPLiveDeatilTableViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/21.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPLiveDeatilTableViewController.h"
#import "SPCommon.h"
#import "SPUpcomingSectionHeaderView.h"
#import "SPLiveDetailHeaderView.h"
#import "SPLiveDetailRoomHeaderFooterView.h"
#import "SPLiveDetailGoodsHeaderFooterView.h"
#import "SPLiveDetailAreaHeaderFooterView.h"
#import "SPLiveDetailPFHeaderFooterView.h"
#import "SPLiveDetailModel.h"



@interface SPLiveDeatilTableViewController ()
@property (nonatomic,strong) SPLiveDetailHeaderView *headerV;
@property (nonatomic,strong) SPLiveDetailModel *live_detail;
@property (strong, nonatomic) NSArray<NSString *> *sections;
@property (assign, nonatomic) GenderType genderType;
@property (strong, nonatomic) NSArray *man;
@property (strong, nonatomic) NSArray *woman;

@property (assign, nonatomic) CGFloat pf_h_woman;
@property (assign, nonatomic) CGFloat pf_h_man;
@property (nonatomic,assign) CGFloat pf_height;
@property (assign, nonatomic) CGFloat area_height;


@end

@implementation SPLiveDeatilTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"直播详情";
    [self setupTableView];
    _sections = @[@"直播商品",@"人流分析",@"",@""];
    _genderType = GenderType_WOMAN;
    _man = @[@"18-25岁",@"25-30岁",@"30-40岁",@"40-50岁",@"50-60岁"];
    _woman = @[@"18-25岁",@"25-30岁",@"30-40岁",@"40-50岁"];
    
    [self fetchData];
}


- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.tableHeaderView.height = 333;
}


- (void)setupTableView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    SPLiveDetailHeaderView *headerV = [SPLiveDetailHeaderView shareHeaderView];
    _headerV = headerV;
    self.tableView.tableHeaderView = headerV;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.height  =10;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPUpcomingSectionHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];
    
}




#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.01;
    if(section == 0 || section ==1){
        height = 45;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
    return 200;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    switch (section) {
        case 0:
//            height = 190;
            height = self.live_detail.goods_list.count*85+20;
            break;
        case 1:
            height = 246;
            break;
        case 2:
//            height = _genderType == GenderType_MAN?330:255;
//            height = UITableViewAutomaticDimension;
            _pf_height = (_genderType==GenderType_WOMAN)?_pf_h_woman:_pf_h_man;
            height = _pf_height == 0?0.01:_pf_height;
            break;
        case 3:
//            height = 246;
            height = _area_height==0?10:_area_height;
            break;
    }
    return height;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0.01;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = nil;
    __weak typeof(self) weakSelf = self;
    switch (section) {
        case 0:{
            SPLiveDetailGoodsHeaderFooterView *goodsV = [SPLiveDetailGoodsHeaderFooterView shareHeaderView];
            goodsV.goods_list = self.live_detail.goods_list;
            footer = goodsV;
            break;
        }
            
        case 1:{
            SPLiveDetailRoomHeaderFooterView *roomV = [SPLiveDetailRoomHeaderFooterView shareHeaderView];
            roomV.model = self.live_detail;
            footer = roomV;
            break;
        }
            
        case 2:{
            SPLiveDetailPFHeaderFooterView *pfV = [SPLiveDetailPFHeaderFooterView shareHeaderView];
            pfV.refreshBlock = ^(GenderType type) {//切换性别刷新数据
                weakSelf.genderType = type;
                [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
            };
            pfV.man = self.man;
            pfV.woman = self.woman;
            pfV.genderType = self.genderType;
            pfV.model = self.live_detail;
            footer = pfV;
            break;
        }
            
        case 3:{
            SPLiveDetailAreaHeaderFooterView *areaV = [SPLiveDetailAreaHeaderFooterView shareHeaderView];
            areaV.total_man_city = self.live_detail.total_man_city;
            footer = areaV;
            break;
        }
    }
    
    return footer;
}




- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = nil;
    if(section == 0 || section == 1){
        SPUpcomingSectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([SPUpcomingSectionHeaderView class])];
        headerV.title.text = _sections[section];
        headerV.moreBtn.hidden = YES;
        view = headerV;
    }
    return view;
}



#pragma mark--
- (void)fetchData
{
    /**
     http://dev.sharepanzer.com/api/bzapi.php?i=10&c=entry&ctrl=live_merch&ac=member&op=member&m=bz_activities&ta=live_detail&token=568e86c8630f7b44f29a23ba211d9f25&act_id=167
     参数 act_id 活动id
     
     */
    
    HelenParametersDic *dict = [HelenParametersDic h_dictionary];
    dict[@"ac"] = @"member";
    dict[@"op"] = @"member";
    dict[@"ta"] = @"live_detail";
    dict[@"act_id"] = @(_act_id);
    dict[@"token"] = [RZ_USERDEFAULTES objectForKey:BUSSION_INFO][@"token"];
    
    [HelenHttpClient requestWithUrlString:BASE_URL andParam:dict andMethod:@"POST" progress:nil success:^(id responseObject) {
        
        NSDictionary *res = responseObject[@"message"];
        NSInteger code = [res[@"resultCode"] integerValue];
        if(code== 0 || code ==1){
          self.live_detail = [SPLiveDetailModel mj_objectWithKeyValues:res[@"data"]];
          [self handleData];
        }
        
    } andFailBlock:^(NSInteger ErrorCode, NSError *ErrorString) {
        
    }];
}



- (void)handleData
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //计算高度
        //地区
        NSInteger count_area = self.live_detail.total_man_city.count;
        NSInteger row_area = count_area / 4;
        if(count_area == 0){
            self.area_height = 85;
        }else if (count_area % 4 == 0){
            self.area_height = 85 + (35+45)*row_area;
        }else if (count_area % 4 != 0){
            self.area_height = 85 + (35+45)*(row_area+1);
        }
        
        //人流
        NSInteger count_pf_woman = self.woman.count;
        NSInteger row_pf_woman = count_pf_woman / 4;
        if(count_pf_woman == 0){
            self.pf_h_woman = 170;
        }else if (count_pf_woman % 4 == 0){
            self.pf_h_woman = 170 + (35+45)*row_pf_woman;
        }else if (count_pf_woman % 4 != 0){
            self.pf_h_woman = 170 + (35+45)*(row_pf_woman+1);
        }
        
        NSInteger count_pf_man = self.man.count;
        NSInteger row_pf_man = count_pf_man / 4;
        if(count_pf_man == 0){
            self.pf_h_man = 170;
        }else if (count_pf_man % 4 == 0){
            self.pf_h_man = 170 + (35+45)*row_pf_man;
        }else if (count_pf_man % 4 != 0){
            self.pf_h_man = 170 + (35+45)*(row_pf_man+1);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.headerV.model = self.live_detail;
            [self.tableView reloadData];
        });
        
    });
    
}



#pragma mark--setter&&getter

@end
